<template>
  <div>
    <el-bread-crumb secondtitle="权限管理" thirdtitle="权限列表"></el-bread-crumb>
    <el-card-comon>
      <template>

        <!-- 表格 -->
        <right-table :rightList="rightList"></right-table>

        <!-- 分页 -->
        <el-pagination-com 
          :nowPage.sync="nowPage" 
          :total="totle" 
          :pageNumber.sync="pageNumber"
          :changeSizeName="'getRightList'"
          :changeSizeNumberName="'getRightList'"></el-pagination-com>

      </template>
    </el-card-comon>
  </div>
</template>

<script>
// 网络请求
import { getAllRight } from '@/network/rightControlRequest';

// 引入组件
import ElBreadCrumb from '@/components/common/ElBreadCrumb';
import ElCardComon from '@/components/common/ElCardComon';
import RightTable from '@/views/Main/rightList/rightListComponents/RightTable';
import ElPaginationCom from '@/components/common/ElPaginationCom';

export default {
  data() {
    return {
      rightList: [], // 权限数据
      nowPage: 1, // 当前第多少页
      pageNumber: 10, // 当前页面显示10条
      totle: 0, // 数据总数
      savePage: 0 // 保存第多少条
    }
  },
  created() {
    this.getAllRightFunc();
    this.$bus.$on('getRightList', this.getAllRightFunc);
  },
  destroyed() {
    this.$bus.$off('getRightList');
  },
  methods: {
    async getAllRightFunc() { // 获取所有权限管理列表
      const {data, meta} = await getAllRight();
      if (meta.status !== 200) { // 异常
        return this.$message.error('网络请求失败' + meta.msg);
      }
      this.rightList = []; // 清空
      this.totle = data.length; // 数据总长
      const index = (this.nowPage - 1) * this.pageNumber; // index算法勿动
      for (; this.savePage < this.pageNumber; this.savePage++) {
        if (!data[index + this.savePage]) {
          break;
        }
        this.rightList.push(data[index + this.savePage]);
        // console.log(data[index], this.savePage);
      }
      // console.log(this.rightList);
      this.savePage = 0;

      // console.log(data);
      return this.$message.success(meta.msg);
    }
  },
  components: {
    ElBreadCrumb,
    ElCardComon,
    RightTable,
    ElPaginationCom
  }
}
</script>

<style lang="scss" scoped>

</style>